package com.chen.springboot.controller;

import com.chen.springboot.common.Result;
import com.chen.springboot.entity.ItemImage;
import com.chen.springboot.service.ItemImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品图片接口")
@RestController
@RequestMapping("/itemImage")
public class ItemImageController {

    @Autowired
    private ItemImageService itemImageService;

    @ApiOperation("根据商品ID获取图片列表")
    @GetMapping("/list")
    public Result<List<ItemImage>> getImagesByItemId(@RequestParam Long itemId) {
        List<ItemImage> images = itemImageService.getImagesByItemId(itemId);
        return Result.success(images);
    }

    @ApiOperation("添加商品图片")
    @PostMapping("/add")
    public Result<String> addImage(@RequestBody ItemImage itemImage) {
        boolean success = itemImageService.save(itemImage);
        return success ? Result.success("添加成功") : Result.error("-1", "添加失败");
    }

    @ApiOperation("批量添加商品图片")
    @PostMapping("/addBatch")
    public Result<String> addBatch(@RequestBody List<ItemImage> itemImages) {
        boolean success = itemImageService.saveBatch(itemImages);
        return success ? Result.success("批量添加成功") : Result.error("-1", "批量添加失败");
    }

    @ApiOperation("删除商品图片")
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteImage(@PathVariable Long id) {
        boolean success = itemImageService.removeById(id);
        return success ? Result.success("删除成功") : Result.error("-1", "删除失败");
    }

    @ApiOperation("更新商品图片")
    @PutMapping("/update")
    public Result<String> updateImage(@RequestBody ItemImage itemImage) {
        boolean success = itemImageService.updateById(itemImage);
        return success ? Result.success("更新成功") : Result.error("-1", "更新失败");
    }
}

